package com.example.previewvideo.util;

import android.content.Context;
import android.media.MediaRecorder;
import android.os.Handler;
import android.os.Looper;

import com.blankj.utilcode.util.AppUtils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MediaRecorderManager {
    private MediaRecorder mediaRecorder;
    private Handler handler;
    private Runnable restartRecordingRunnable;
    private static final int MAX_DURATION_MS = 60000; // 最大录制时间，例如60秒
    Context context;
    int  count = 0;
    public MediaRecorderManager(Context context) {
        handler = new Handler(Looper.getMainLooper());
        restartRecordingRunnable = new Runnable() {
            @Override
            public void run() {
                count++;
                startRecording();
            }
        };
    }
    private File getOutputMediaFile() {
        String directoryPath = context.getCacheDir().getAbsolutePath() + "/videoNameA/";
        File mediaStorageDir = new File(directoryPath);
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                return null;
            }
        }

        String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        return new File(mediaStorageDir.getPath() + File.separator +
                "VID_" + timestamp + "_"  +count+ ".mp4");
    }
    public void startRecording() {
        if (mediaRecorder != null) {
            mediaRecorder.stop();
            mediaRecorder.reset();
            mediaRecorder.release();
        }

        mediaRecorder = new MediaRecorder();
        // 配置MediaRecorder
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mediaRecorder.setOutputFile(getOutputMediaFile().getAbsolutePath());
        mediaRecorder.setMaxDuration(MAX_DURATION_MS);

        mediaRecorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {
            @Override
            public void onInfo(MediaRecorder mr, int what, int extra) {
                if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
                    // 达到最大录制时间，停止录制
                    mediaRecorder.stop();
                    mediaRecorder.reset();
                    mediaRecorder.release();
                    // 启动新的录制
                    handler.post(restartRecordingRunnable);
                }
            }
        });

        try {
            mediaRecorder.prepare();
            mediaRecorder.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void stopRecording() {
        if (mediaRecorder != null) {
            mediaRecorder.stop();
            mediaRecorder.reset();
            mediaRecorder.release();
            mediaRecorder = null;
        }
        handler.removeCallbacks(restartRecordingRunnable);
    }
}
